package com.qst.rongxiaotong.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.rongxiaotong.mapper.KnowledgeMapper;
import com.qst.rongxiaotong.pojo.Knowledge;
import com.qst.rongxiaotong.service.KnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class KnowledgeServiceImpl implements KnowledgeService {
    //每页显示多条数据
    private static final Integer pageSize = 30;
    @Autowired
    KnowledgeMapper knowledgeMapper;


    //分页查询Knowledge对象
    @Override
    public PageInfo<Knowledge> findPage(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<Knowledge> knowledges = knowledgeMapper.selectAll();
        PageInfo<Knowledge> knowledgePageInfo = new PageInfo<>(knowledges);
        return knowledgePageInfo;
    }

    //根据Id查询信息
    @Override
    public Knowledge selectById(Integer id) {
        Knowledge knowledge = knowledgeMapper.selectByPrimaryKey(id);
        return knowledge;
    }

    @Override
    public PageInfo<Knowledge> findPageByKeys(String keys, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<Knowledge> knowledges = knowledgeMapper.selectAllByKeys(keys);
        PageInfo<Knowledge> knowledgePageInfo = new PageInfo<>(knowledges);
        return knowledgePageInfo;
    }

    @Override
    public void add(Knowledge knowledge) {
        //获取用户登录名
        //UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //String username = userDetails.getUsername();
        //knowledge.setOwnName(username).setCreateTime(new Date()).setUpdateTime(new Date());
        knowledge.setCreateTime(new Date());
        knowledge.setUpdateTime(new Date());
        knowledgeMapper.insertSelective(knowledge);
    }

    @Override
    public void update(Knowledge knowledge,Integer id) {
        knowledge.setKnowledgeId(id);
        knowledgeMapper.updateByPrimaryKeySelective(knowledge);
    }

    @Override
    public void delete(Integer id) {
        knowledgeMapper.deleteByPrimaryKey(id);
    }

    //根据登录用户查询个人农业知识
    @Override
    public List<Knowledge> selectByUsername(String name) {
        List<Knowledge> knowledges = knowledgeMapper.selectByExample(name);
        return knowledges;
    }
}
